/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import entity.MemberCustomer;
import entity.MemberCustomerFacadeLocal;
import entity.UserLogin;
import entity.UserLoginFacadeLocal;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

/**
 *
 * @author SONY
 */
@Stateless
public class MemberCustomerService implements MemberCustomerServiceLocal {
    
    @EJB MemberCustomerFacadeLocal memberCustomerFacadeLocal;
    @EJB UserLoginFacadeLocal userLoginFacadeLocal;

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public boolean addMember_UserLogin(String firstname, String lastname, String address, String phone, String gender, String email, String password) {
        try {
            //add Member info into Member table
            MemberCustomer member = new MemberCustomer();
            member.setMemberID(1);
            member.setFirstName(firstname);
            member.setLastName(lastname);
            member.setAddress(address);
            member.setPhone(phone);
            member.setGender(gender);
            memberCustomerFacadeLocal.create(member);
            
            //get Member object just add into table
            List<MemberCustomer> lstMembers = memberCustomerFacadeLocal.findAll();
            int index = lstMembers.size() - 1;
            MemberCustomer member_new = lstMembers.get(index);
            
            //add UserLogin info into User_Login table
            UserLogin user = new UserLogin();
            user.setEmailAddress(email);
            user.setPassword(password);
            user.setMemberID(member_new);
            userLoginFacadeLocal.create(user);
            
            return true;
        } catch (Exception e) {
            System.err.println(e);
        }
        return false;
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public boolean updateMember_UserLogin(int memberid, String firstname, String lastname, String address, String phone, String gender, String email, String password, boolean isChangePass) {
        try {
            //update Member info into Member table
            MemberCustomer member = new MemberCustomer();
            member.setMemberID(memberid);
            member.setFirstName(firstname);
            member.setLastName(lastname);
            member.setAddress(address);
            member.setPhone(phone);
            member.setGender(gender);
            memberCustomerFacadeLocal.edit(member);
            if(isChangePass) {
                //update UserLogin info into UserLogin table
                UserLogin user = new UserLogin();
                user.setEmailAddress(email);
                user.setPassword(password);
                user.setMemberID(member);
                userLoginFacadeLocal.edit(user);
            }
            return true;
        } catch (Exception e) {
            System.err.println(e);
        }
        return false;
    }

}
